<template>
    <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" class="demo-ruleForm">
        <el-card shadow="always" class="outbox">
            <el-form-item prop="loginName">
                <h1 class="title">嗨购平台账号注册</h1>
                <el-input v-model="ruleForm.loginName" type="text" autocomplete="off">
                    <template #prefix>
                        <el-icon>
                            <User />
                        </el-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="ruleForm.password" type="password" autocomplete="off">
                    <template #prefix>
                        <el-icon>
                            <Lock />
                        </el-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="checkPass">
                <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off">
                    <template #prefix>
                        <el-icon>
                            <Lock />
                        </el-icon>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">确定注册</el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-card>
    </el-form>
</template>
  
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import {router} from '../router';
import { login } from '../services/index'

const ruleFormRef = ref<FormInstance>()

const checkLoginName = (rule: any, value: any, callback: any)=>{
    const r = /^[^\u4e00-\u9fa5]+$/
    if (value === '') {
        callback(new Error('未填写用户名'))
        }else if(!r.test(ruleForm.loginName)){
            callback(new Error('用户名不能为中文'))
        } else {
            callback()
        }
    
}

const password = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('未填写密码'))
    } else {
        callback()
    }
}

const checkPass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('未填写密码'))
    } else {
        callback()
    }
    console.log(ruleForm.password,'ruleForm');
    
}

const ruleForm = reactive({
    loginName: '',
    password: '',
    checkPass:''
})

const rules = reactive({
    loginName: [{ validator: checkLoginName, trigger: 'change' }],
    password: [{ validator: password, trigger: 'blur' }],
    checkPass: [{ validator: checkPass, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            // ruleForm.pass === 'admin' && ruleForm.checkPass === 'admin' && router.push('/MainPage') || alert('账号密码为admin')
            login({
                 adminname: ruleForm.loginName,
                 password: ruleForm.password
            }).then((result) => { 
            if (result.data.code === '200') {
                localStorage.setItem('token',result.data.data.token)
            } 
            })
        } else {
            console.log('error submit!')
            return false
        }
    })
}
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return

}

</script>

<style scoped>
.demo-ruleForm {
    width: 340px;
    margin: 200px auto auto;
    padding: 50px;
}
.title {
    text-align: center;
    font-size: 28px;
    font-weight: bold;
    margin-bottom: 20px;
}
.outbox{
    border-radius: 30px;
}
</style>